package com.ywu.bpmn.flow.consumer.process;

import com.ywu.bpmn.flow.consumer.YwuFlowConsumer;
import com.ywu.bpmn.flow.data.YwuFlowInstanceRepository;
import com.ywu.bpmn.flow.entity.YwuFlowTaskEntity;
import com.ywu.bpmn.spi.dto.YwuBpmnMqDto;
import com.ywu.common.gson.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName YwuFLowTaskCompleteConsumer
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/23 21:05
 * @Version 1.0
 **/
@Component
@Slf4j
public class YwuFLowTaskCompleteConsumer implements YwuFlowConsumer {
    @Autowired
    private YwuFlowInstanceRepository repository;

    private static final String TASK_COMPLETE = "task-complete";

    @Override
    public String type() {
        return TASK_COMPLETE;
    }

    @Override
    public void process(String key, String value) {
        log.debug("do consumer task complete");
        // 将json串反序列化
        YwuBpmnMqDto deserialize = GsonUtils.deserialize(value, YwuBpmnMqDto.class);
        // 修改审批结点的状态，为审批完成
        YwuFlowTaskEntity ywuFlowTaskEntity = new YwuFlowTaskEntity();
        ywuFlowTaskEntity.setTaskId(deserialize.getTaskId());
        ywuFlowTaskEntity.setFlowInstanceId(deserialize.getInstanceId());
        ywuFlowTaskEntity.setStatus(YwuFlowTaskEntity.FINISH_STATUS);
        ywuFlowTaskEntity.updateInfo(deserialize.getAssignee());
        repository.updateTaskStatus(ywuFlowTaskEntity);
    }
}
